﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Http;
using FellowshipOne.API;
using FellowshipOne.API.Base;
using FellowshipOne.API.Events.Model;
using MasVida.WebApi.Controllers.Interfaces;
using MasVida.WebApi.Models.DataObject;
using MasVida.WebApi.Models.Requests;
using MasVida.WebApi.Models.Responses;
using Newtonsoft.Json.Linq;

namespace MasVida.WebApi.Controllers
{
    public class AttendanceController : ApiBaseController
    {
        public AttendanceController()
        {

        }

        public AttendanceController(IRestClient restClient)
        {
            RestClient = restClient;
        }

        [HttpPost]
        public AttendanceResponse GetAttendancesByAttendanceSummaryId(JObject jObject)
        {
            AttendanceRequest attendanceRequest = GetRequestFromJObject(jObject);

            AttendanceResponse response = new AttendanceResponse();

            try
            {
                IRestClient restClient = GetRestClient(attendanceRequest.AccessTicket);
                AttendanceCollection attendances = restClient.EventsRealm.AttendanceCollection.Get(attendanceRequest.AttendanceSummaryId);
                response.Attendances = attendances.Select(a => new AttendanceDO()
                {
                    PersonId = a.Person.ID ?? 0,
                    AttendanceId = a.ID ?? 0,
                    IsPresent = a.IsPresent,
                    Person = new PersonDO(restClient.PeopleRealm.People.Get(a.Person.ID.ToString()))
                })
                .OrderBy(a => a.Person.Name);
            }
            catch (Exception ex)
            {
                response.ErrorMessage = ex.Message;
            }
            return response;
        }

        [HttpPost]
        public AttendanceResponse UpdateAttendances(JObject jObject)
        {
            AttendanceRequest attendanceRequest = GetRequestFromJObject(jObject);

            AttendanceResponse response = new AttendanceResponse();

            try
            {
                IRestClient restClient = GetRestClient(attendanceRequest.AccessTicket);
                AttendanceCollection attendances = restClient.EventsRealm.AttendanceCollection.Get(attendanceRequest.AttendanceSummaryId);

                foreach (AttendanceDO attendanceDo in attendanceRequest.Attendances)
                {
                    Attendance attendance =
                        attendances.SingleOrDefault(a => a.ID != null && a.ID == attendanceDo.AttendanceId);

                    if (attendance == null) continue;

                    attendance.IsPresent = attendanceDo.IsPresent;
                    attendance.UpdatedDate = DateTime.Now;
                    restClient.EventsRealm.Attendance.Update(attendance, attendance.ID.ToString(), attendanceRequest.AttendanceSummaryId);
                }
            }
            catch (Exception ex)
            {
                response.ErrorMessage = ex.Message;
            }
            return response;
        }

        private AttendanceRequest GetRequestFromJObject(JObject jobject)
        {
            return new AttendanceRequest()
            {
                ApiKey = jobject["ApiKey"]?.Value<string>(),
                AccessTicket = jobject["AccessTicket"]?.ToObject<F1OAuthTicket>(),
                AttendanceSummaryId = jobject["AttendanceSummaryId"]?.Value<string>(),
                Attendances = jobject["Attendances"]?.ToObject<IEnumerable<AttendanceDO>>()
            };
        }
    }
}